package com.zhou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.dto.dictionary.AddDictionaryDTO;
import com.zhou.dto.dictionary.UpdateDictionaryDTO;
import com.zhou.entity.Dictionary;
import com.zhou.mapper.DictionaryMapper;
import com.zhou.service.DictionaryService;
import com.zhou.utils.APPApplicationException;
import com.zhou.utils.BeanChangeUtils;
import com.zhou.utils.enums.BooleanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import static com.zhou.utils.CommonCast.NO;

/**
 * @Description : 字典
 * @Author : zyy
 * @Date: 2024/6/17  15:15
 */
@Service
public class DictionaryServiceImpl implements DictionaryService {
    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Override
    public List<Dictionary> getList(Integer page, Integer pageSize, String dictionaryType) {
        LambdaQueryWrapper<Dictionary> wrapper = new LambdaQueryWrapper<>();
        Page<Dictionary> dictionaryIPage = new Page<>(page, pageSize);
        wrapper.eq(Dictionary::getDictionaryType, dictionaryType);
        return dictionaryMapper.selectPage(dictionaryIPage, wrapper).getRecords();
    }

    @Override
    public Integer add(AddDictionaryDTO dto) {
        if (dictionaryMapper.selectOne(new LambdaQueryWrapper<Dictionary>().eq(Dictionary::getDictionaryType, dto.getDictionaryType())) != null) {
            throw new APPApplicationException("该类型已存在");
        }
        Dictionary dictionary = new Dictionary();
        dictionary.setDictionaryId(UUID.randomUUID().toString());
        dictionary.setCreateTime(new Date());
        dictionary.setDictionaryType(dto.getDictionaryType());
        dictionary.setDictionaryDescribe(dto.getDictionaryDescribe());
        if (BeanChangeUtils.isNotEmpty(dto.getFatherId())) {
            //父级id不为空，就说明是子级
            dictionary.setFatherId(dto.getFatherId());
        }
        return dictionaryMapper.insert(dictionary);
    }

    @Override
    public String update(UpdateDictionaryDTO dto) {
        Dictionary dictionary = dictionaryMapper.selectById(dto.getDictionaryId());
        if (BeanChangeUtils.isEmpty(dictionary)) {
            throw new APPApplicationException("没有查询到数据，请重试");
        }
        dictionary.setUpdateTime(new Date());
        dictionary.setDictionaryType(dto.getDictionaryType());
        dictionary.setDictionaryDescribe(dto.getDictionaryDescribe());
        int num = dictionaryMapper.updateById(dictionary);
        if (num == 0) {
            return "修改失败";
        }
        return "修改成功";
    }

    @Override
    public String deleteStatus(String dictionaryId) {
        Dictionary dictionary = dictionaryMapper.selectById(dictionaryId);
        if (BeanChangeUtils.isEmpty(dictionary)) {
            throw new APPApplicationException("没有该条记录请重试");
        }
        //如果有下级就不能直接删除，得先把下级删除
        List<Dictionary> dictionaries = dictionaryMapper.selectList(new LambdaQueryWrapper<Dictionary>().eq(Dictionary::getFatherId, dictionaryId));
        if (BeanChangeUtils.isNotEmpty(dictionaries)) {
            throw new APPApplicationException("该字典类型有下级不能直接删除，请先删除下级在进行删除");
        }
        dictionary.setDelFlag(BooleanEnum.TURE.getResultValue());
        int i = dictionaryMapper.updateById(dictionary);
        if (i == NO) {
            return "删除失败";
        }
        return "删除成功";
    }

    @Override
    public String delete(String dictionaryId) {
        Dictionary dictionary = dictionaryMapper.selectById(dictionaryId);
        if (BeanChangeUtils.isEmpty(dictionary)) {
            throw new APPApplicationException("没有记录，请重试");
        }
        List<Dictionary> dictionaries = dictionaryMapper.selectList(new LambdaQueryWrapper<Dictionary>().eq(Dictionary::getFatherId, dictionaryId));
        if (BeanChangeUtils.isNotEmpty(dictionaries)) {
            throw new APPApplicationException("该字典类型有下级不能直接删除，请先删除下级在进行删除");
        }
        int i = dictionaryMapper.deleteById(dictionaryId);
        if (i == NO) {
            return "删除失败";
        }
        return "删除成功";
    }
}
